﻿#include "PolarGraph.h"

#include "LineChartTracer.h"

namespace pst
{
    PolarGraph::PolarGraph(QCPPolarAxisAngular* keyAxis, QCPPolarAxisRadial* valueAxis)
        : QCPPolarGraph(keyAxis, valueAxis)
        , m_selectedPen(QColor(80, 80, 255))
        , m_normalPen(mPen)
    {
        m_selectedPen.setWidth(2);

        // 自动获取图例
        QCPLegend* lengend = keyAxis->parentPlot()->legend;
        if (lengend && lengend->itemCount() != 0) {
            m_legendItem = lengend->item(lengend->itemCount() - 1);
            connect(m_legendItem, &QCPAbstractLegendItem::selectionChanged, this, [this](bool selected) {
                setSelected(selected);
                Q_EMIT signal_legendItemSelectionChanged(selected);
            });
        }

        // 自动更新选中/非选中样式
        connect(this, QOverload<bool>::of(&QCPPolarGraph::selectionChanged), this, &PolarGraph::slot_setSelected);
    }

    PolarGraph::~PolarGraph()
    {
    }

    void PolarGraph::setSelectedPen(const QPen& pen)
    {
        m_selectedPen = pen;
    }

    void PolarGraph::setNormalPen(const QPen& pen)
    {
        m_normalPen = pen;
        if (!isSelected()) {
            setPen(pen);
        }
    }

    void PolarGraph::slot_setSelected(bool selected)
    {
        if (m_isSelected == selected)
        {
            return;
        }
        setSelected(selected);
        Q_EMIT signal_selectionChanged(m_isSelected);
    }

    void PolarGraph::updateSelectionState(bool selected)
    {
        if (m_isSelected)
        {
            m_normalPen = std::move(mPen);
            setPen(m_selectedPen);
        }
        else
        {
            setPen(m_normalPen);
        }
    }

    void PolarGraph::mouseDoubleClickEvent(QMouseEvent* event, const QVariant& details)
    {
        Q_EMIT signal_mouseDoubleClicked(event, details); 
    }

    QCPAbstractLegendItem* PolarGraph::getLegendItem() const
    {
        return m_legendItem;
    }

    QPen PolarGraph::getSelectedPen() const
    {
        return m_selectedPen;
    }

    QPen PolarGraph::getNormalPen() const
    {
        return m_normalPen;
    }

    bool PolarGraph::isSelected() const
    {
        return m_isSelected;
    }

    void PolarGraph::setSelected(bool selected)
    {
        if (m_isSelected == selected)
        {
            return;
        }
        m_isSelected = selected;

        updateSelectionState(selected);

        if (m_legendItem && m_legendItem->selected() != selected)
        {
            m_legendItem->setSelected(selected);
        }
    }

    void PolarGraph::addTracer(LineChartTracer* tracer)
    {
        tracer->setPolarGraph(this);
        m_tracerList.append(tracer);
    }
    void PolarGraph::removeTracer(LineChartTracer* tracer)
    {
        if (m_tracerList.removeOne(tracer)) {
            tracer->setPolarGraph(nullptr);
        }
    }

    QList<LineChartTracer*> PolarGraph::getAllTracer()
    {
        return m_tracerList;
    }

    void PolarGraph::removeAllTracer()
    {
        for (auto item : m_tracerList)
        {
            item->setPolarGraph(nullptr);
        }
        m_tracerList.clear();
    }

    void PolarGraph::setVisible(bool on)
    {
        QCPPolarGraph::setVisible(on);
        for (auto item : m_tracerList) {
            item->setVisible(on);
        }
    }
}
